package com.upb.webserver.rest.fisco;

import com.upb.webserver.common.constant.RestConstant;
import com.upb.webserver.common.pojo.base.BasePageResponse;
import com.upb.webserver.common.pojo.base.BaseResponse;
import com.upb.webserver.common.properties.RestProperties;
import com.upb.webserver.common.tools.HttpRequestTools;
import com.upb.webserver.rest.AbstractRestService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.util.Map;

@Log4j2
@Service
public class FiscoStatRestService extends AbstractRestService {
    @Autowired
    private RestProperties restProperties;
    private String serverName = RestConstant.SERVER_NAME_WEBASE_STAT;

    @PostConstruct
    public void init() {
        initServerAddressMap(serverName, restProperties.getWebaseStatAddressList());
    }

    public BasePageResponse getGroupBasicData(Integer chainId, Integer frontId, LocalDateTime beginDate,
                                              LocalDateTime endDate, Integer pageNumber, Integer pageSize, Integer groupId) {
        Map<String, String> param = HttpRequestTools.getStatQueryParam(chainId, frontId, beginDate,
                endDate, pageNumber, pageSize, groupId);
        String uri = HttpRequestTools.getQueryUri(RestConstant.URI_FISCO_STAT_GROUP_BASIC_DATA, param);
        BasePageResponse frontRsp = httpGet(uri, BasePageResponse.class);
        return frontRsp;
    }

    public BasePageResponse getNodeMonitor(Integer chainId, Integer frontId, LocalDateTime beginDate,
                                           LocalDateTime endDate, LocalDateTime contrastBeginDate,
                                           LocalDateTime contrastEndDate, Integer gap, Integer groupId) {
        Map<String, String> param = HttpRequestTools.getStatQueryParam(chainId, frontId, beginDate,
                endDate, contrastBeginDate, contrastEndDate, gap, groupId);
        String uri = HttpRequestTools.getQueryUri(RestConstant.URI_FISCO_STAT_NODE_MONITOR, param);
        BasePageResponse frontRsp = httpGet(uri, BasePageResponse.class);
        return frontRsp;
    }

    public BasePageResponse getServerPerformance(Integer chainId, Integer frontId, LocalDateTime beginDate, LocalDateTime endDate,
                                                 LocalDateTime contrastBeginDate, LocalDateTime contrastEndDate, Integer gap) {
        Map<String, String> param = HttpRequestTools.getStatQueryParam(chainId, frontId, beginDate,
                endDate, contrastBeginDate, contrastEndDate, gap, null);
        String uri = HttpRequestTools.getQueryUri(RestConstant.URI_FISCO_STAT_SERVER_PERFORMANCE, param);
        BasePageResponse frontRsp = httpGet(uri, BasePageResponse.class);
        return frontRsp;
    }



    @Override
    public <T> T httpGet(String uri, Class<T> clazz) {
        return super.restTemplateExchange(serverName, RestConstant.FISCO_STAT_URL_FORMAT, uri, HttpMethod.GET, null, clazz);
    }

    @Override
    public <T> T httpPost(String uri, Object param, Class<T> clazz) {
        return super.restTemplateExchange(serverName, RestConstant.FISCO_STAT_URL_FORMAT, uri, HttpMethod.POST, param, clazz);
    }

    @Override
    public <T> T httpPut(String uri, Object param, Class<T> clazz) {
        return super.restTemplateExchange(serverName, RestConstant.FISCO_STAT_URL_FORMAT, uri, HttpMethod.PUT, param, clazz);
    }

    @Override
    public <T> T httpDelete(String uri, Object param, Class<T> clazz) {
        return super.restTemplateExchange(serverName, RestConstant.FISCO_STAT_URL_FORMAT, uri, HttpMethod.DELETE, param, clazz);
    }
}
